package com.baizhi.controller;

import com.baizhi.entity.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/captchas")
public class SmsController implements Serializable {


    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public void Code(@RequestBody PhoneCode phoneCode){
        Random r = new Random();
        String str = "";//列出所有的字母数字
        for (int i = 0; i < 4; i++) {  //循环4次，输出四个数
            int a = r.nextInt(10);//从0-61中随机一个数，作为字符串的索引
            str = str + a;
//            System.out.println(str.substring(a, a + 1));//从字符串中利用索引找到输出它

        }
        System.out.println(str);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(phoneCode.getPhone(),str,1, TimeUnit.MINUTES);
    }


}
